package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"tmall-res-mgr/chone"
	"tmall-res-mgr/modler"
)

func CarAdd(c *gin.Context) {
	CarName := c.PostForm("Car_name")
	CarCode := c.PostForm("Car_code")
	CarPrice := c.PostForm("Car_Prince")
	if CarName == "" || CarCode == "" || CarPrice == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := modler.Car{

		CarName:   CarName,
		CarCode:   CarCode,
		CarPrince: CarPrice,
	}
	err := modler.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	redis_key := "CarAdd" + strconv.Itoa(int(data.ID))
	var list modler.Car
	modler.DB.Where("id=?", data.ID).First(&list)
	marshal, _ := json.Marshal(list)
	chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarOrder(c *gin.Context) {
	CarOrderName := c.PostForm("CarOrderName")
	CarOrderPrince := c.PostForm("CarOrderPrince")
	Cid, _ := strconv.Atoi(c.PostForm("cid"))
	if CarOrderName == "" || CarOrderPrince == "" || Cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := modler.CarOrder{

		CarOrderName:   CarOrderName,
		CarOrderPrince: CarOrderPrince,
		Cid:            Cid,
	}
	err := modler.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	redis_key := "CarOrder" + strconv.Itoa(int(data.ID))
	var list modler.CarOrder
	modler.DB.Where("id=?", data.ID).First(&list)
	marshal, _ := json.Marshal(list)
	chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1) "))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
		SELECT
		  car_orders.id AS OrderId,
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  cars.car_prince AS CarPrince,
		  car_orders.car_order_name AS CarOrderName,
		  car_orders.car_order_prince AS CarOrderPrince
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.cid
	*/
	var list []modler.CarOrderList
	redis_key := "CarList" + strconv.Itoa(page)
	result, _ := chone.Rdb.Get(chone.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.Table("car_orders").
			Select("car_orders.id AS OrderId",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_prince AS CarPrince",
				"car_orders.car_order_name AS CarOrderName",
				"car_orders.car_order_prince AS CarOrderPrince",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Offset(offset).
			Limit(size).
			Find(&list)
		marshal, _ := json.Marshal(list)
		chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}
	var count int64
	modler.DB.Model(&modler.CarOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"count": count,
	})
	return
}

func CarDetail(c *gin.Context) {
	id := c.Query("id")
	var list []modler.CarOrderList
	redis_key := "CarDetail" + id
	result, _ := chone.Rdb.Get(chone.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.Table("car_orders").
			Select("car_orders.id AS OrderId",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_prince AS CarPrince",
				"car_orders.car_order_name AS CarOrderName",
				"car_orders.car_order_prince AS CarOrderPrince",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Where("car_orders.id=?", id).
			Find(&list)
		marshal, _ := json.Marshal(list)
		chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}
	var count int64
	modler.DB.Model(&modler.CarOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
